﻿//        Another Demo from Andy L. & MissedMemo.com
// Borrow whatever code seems useful - just don't try to hold
// me responsible for any ill effects. My demos sometimes use
// licensed images which CANNOT legally be copied and reused.

using System;
using System.Collections.Generic;
using System.ServiceModel;


namespace MissedMemo.MeatAndCheeseDisc.Services
{
    //[ServiceBehavior(ConcurrencyMode=ConcurrencyMode.Reentrant)] (not needed if comms are all OneWay)
    public class KitchenOrderMonitorService : IKitchenOrderMonitorService
    {
        private static List<IOrderMonitorNotification> _subscribers = new List<IOrderMonitorNotification>();


        public void SubscribeOrderMonitorNotifications()
        {
            IOrderMonitorNotification subscriber = OperationContext.Current.GetCallbackChannel<IOrderMonitorNotification>();

            _subscribers.Add( subscriber );
        }


        public void UnsubscribeOrderMonitorNotifications()
        {
            IOrderMonitorNotification subscriber = OperationContext.Current.GetCallbackChannel<IOrderMonitorNotification>();

            _subscribers.Remove( subscriber );
        }


        // Relay finished pizza info. to order manager apps

        public void NotifyPizzaIsDone( int nowServingNumber, Pizza pizza )
        {
            OrderEntryManagementService.NotifyPizzaIsDone( nowServingNumber, pizza );
        }


        // Relay new pizza order to all (probably just one) kitchen monitors

        internal static void ProcessNewOrder( int nowServingNumber, Pizza pizza )
        {
            foreach( IOrderMonitorNotification subscriber in _subscribers )
            {
                subscriber.OnNewPizza( nowServingNumber, pizza );
            }
        }


        // Relay order cancellation to all (probably just one) kitchen monitors

        internal static void CancelOrder( int orderID )
        {
            foreach( IOrderMonitorNotification subscriber in _subscribers )
            {
                subscriber.OnCancelOrder( orderID );
            }
        }
    }
}
